package dper.study.codes.common.tailed;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Collections;
import java.util.Map;

/**
 * Created by leichen on 2017/5/11.
 */
public class MyTailedMemeryCache {
        private Map<String, String> iMap;
        private static Logger logger = LoggerFactory.getLogger(MyTailedMemeryCache.class);

        public MyTailedMemeryCache(int cap) {

            iMap = Collections.synchronizedMap(new MyTailedLinkedHashMap(cap));
        }

        public void put(String key,String value) {
            if(null != key ){
                iMap.put(key, value);
            }
        }

        public String get(String key) {
            return iMap.get(key);
        }

        public boolean containsKey(String key) {
            return this.iMap.containsKey(key);
        }

        /**
         * 每 1 分钟 log 一次 myTailedMemeryCache 情况
         */
        @Scheduled(cron = "0 */1 * * * *")
        public void logIpCache() {
            logger.info(this.iMap.toString());
        }

        /**
         * 每天 0 点清除一次 myTailedMemeryCache
         */
        @Scheduled(cron = "0 0 0 * * *")
        public void clearIpCache() {
            this.iMap.clear();
        }
}
